package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.JSONResult;
import com.qf.pojo.Owner;
import com.qf.service.OwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("owner")
public class OwnerController {
    @Autowired
    private OwnerService ownerService;

    @RequestMapping("findAll")
    public JSONResult findAll(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer pageNum
            ,@RequestParam(value = "limit",required = false,defaultValue = "5") Integer pageSize) {
        JSONResult jsonResult = new JSONResult();
        PageHelper.startPage(pageNum,pageSize);
        List<Owner> Owners = ownerService.findAll();
        PageInfo<Owner> pageInfo = new PageInfo<>(Owners);
        jsonResult.setCode(0);
        jsonResult.setCount(pageInfo.getTotal());
        jsonResult.setMsg("success");
        jsonResult.setData(Owners);
        return jsonResult;
    }

    @RequestMapping("removeOne")
    public String removeOne(Integer id) {
        ownerService.removeOne(id);
        return "success";
    }

    @RequestMapping("removeAll")
    public String removeAll (Integer[] ids) {
        ownerService.removeList(Arrays.asList(ids));
        return "success";
    }

    @RequestMapping("updateOwner")
    public String updateOwner(Owner owner) {
        ownerService.updateOwner(owner);
        return "success";
    }

    @RequestMapping("addOwner")
    public String addOwner(Owner owner) {
        ownerService.addOwner(owner);
        return "success";
    }

    @RequestMapping("findById")
    public Owner findById(Integer id) {
        Owner owner = ownerService.findById(id);
        return owner;
    }
}
